class Solution {
public:
    int minFlips(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        int ret1 = 0, ret2 = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j * 2 < m - 1; j++) {
                if (grid[i][j] != grid[i][m - j - 1]) {
                    ret1++;
                    // cout << i << ' ' << j << "    ";
                }
            }
        }
        // cout << endl;
        for (int i = 0; i * 2 < n - 1; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] != grid[n - i - 1][j]) {
                    ret2++;
                    // cout << i << ' ' << j << "    ";
                }
            }
        }
        return min(ret1, ret2);
    }
};